#ifndef RGBCAPTURE_H
#define RGBCAPTURE_H

#include <QObject>
#include <QThread>
#include <QMutex>
#include <QWaitCondition>

extern "C"
{
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
#include <libavdevice/avdevice.h>
#include <libavformat/version.h>
#include <libavutil/time.h>
#include <libavutil/mathematics.h>
#include <libavutil/imgutils.h>
}

class RgbCapture : public QObject
{
    Q_OBJECT
public:
    explicit RgbCapture(QObject *parent = nullptr);
    ~RgbCapture() override;

    void setConnectTimeout(int ms);
    void setReadTimeout(int ms);
    void setOverallTimeout(int ms);
signals:
    void frameReady(const QImage &frame);
    void error(const QString &message);
    void started();
    void stopped();
    void clearFrame(); // 新增清除帧信号
public slots:
    void start(const QString &url);
    void stop();

private:
    bool m_running = false;
    AVFormatContext *m_formatContext = nullptr;
    AVCodecContext *m_codecContext = nullptr;
    SwsContext *m_swsContext = nullptr;
    int m_videoStreamIndex = -1;

    int m_connectTimeout{ 5000 }; // 连接超时
    int m_readTimeout{ 3000 }; // 读取帧超时
    int m_overallTimeout{ 3000 }; // 整体操作超时
};

#define RgbCaptureRelease Singleton<RgbCapture>::Release()
#define RgbCaptureInstance Singleton<RgbCapture>::GetInstance()

#endif // RGBCAPTURE_H
